package com.ghtmlx.client.grid;

import com.ghtmlx.client.menu.MenuObject;
import com.ghtmlx.client.util.DataType;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Image;

/**
 * User: j.voronin
 * Date: 20.01.2010
 */
public class GridObjectImpl {

    /**
     * Create a JavaScript Grid
     *
     * @param container - add Grid to this HTML-Element
     * @return {@link com.ghtmlx.client.grid.GridObject}
     */
    public static native GridObject create(Element container)/*-{
        $wnd.doOnAfterRefresh = function(){
            //TODO: Java Event here
        }
        $wnd.doOnAfterUpdate = function(){
            //TODO: Java Event here
        }


        var mygrid = new $wnd.dhtmlXGridObject(container);
        return mygrid;
    }-*/;

    /**
     * Attach events to the grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void attachEvents(GridObject grid)/*-{
        grid.attachEvent("doAfterRefresh", $wnd.doOnAfterRefresh);
    }-*/;

    /**
     * Adds row to the grid
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param row_id    - row ID, must be unique
     * @param dataArray - row values, may be a comma separated list or an array
     */
    public static native void addRow(GridObject grid, int row_id, String dataArray)/*-{
        grid.addRow(row_id, dataArray);
    }-*/;

    /**
     * Adds row to the grid to the index position
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param row_id    - row ID, must be unique
     * @param dataArray - row values, may be a comma separated list or an array
     * @param index     - index of new row
     */
    public static native void addRowOnPosition(GridObject grid, int row_id, String dataArray, int index)/*-{
        grid.addRow(id, dataArray, index);
    }-*/;

    /**
     * Adjusts column size to make all content visible
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param cInd - index of column
     */
    public static native void adjustColumnSize(GridObject grid, int cInd)/*-{
         grid.adjustColumnSize(cInd);
    }-*/;

    /**
     * Attach additional line to header.<br>
     * attachHeader() command supports HTML as values, so it may contain any HTML compatible content.
     *
     * @param grid-      JSObject (dhtmlxGrid)
     * @param dataArray  -array of header titles
     * @param styleArray -array of styles ("color:red;"), optional
     */
    public static native void attachHeader(GridObject grid, String dataArray, String styleArray)/*-{
         grid.attachHeader(dataArray, styleArray);
    }-*/;

    /**
     * Attach grid to some object in DOM
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param obj  - object to attach to
     */
    public static native void attachToObject(GridObject grid, JavaScriptObject obj)/*-{
       grid.attachToObject(obj);
    }-*/;

    /**
     * Gets a CellObject (if no arguments then gets CellObject object of currently selected cell)
     *
     * @param grid-  JSObject (dhtmlxGrid)
     * @param row_id - row id
     * @param col    - column index
     * @return {@link com.ghtmlx.client.grid.CellObject}
     */
    public static native CellObject cellById(GridObject grid, int row_id, int col)/*-{
        return grid.cellById(row_id, col);
     }-*/;

    /**
     * Gets a CellObject (if no arguments then gets CellObject object of currently selected cell)
     *
     * @param grid   -  JSObject (dhtmlxGrid)
     * @param row_id - row id
     * @param col    - column index
     * @return {@link com.ghtmlx.client.grid.CellObject}
     */
    public static native CellObject cellByIndex(GridObject grid, int row_id, int col)/*-{
         return  grid.cellById(row_id, col);
    }-*/;

    /**
     * Changes id of the row to the new one
     *
     * @param grid      -  JSObject (dhtmlxGrid)
     * @param old_rowId - row id to change
     * @param new_rowId - row id to set
     */
    public static native void changeRowId(GridObject grid, int old_rowId, int new_rowId)/*-{
         grid.changeRowId(old_rowId, new_rowId);
     }-*/;

    /**
     * Check all checkboxes in grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - check/uncheck all checkboxes in grid (optional, true by default)
     */
    public static native void checkAll(GridObject grid, boolean mode)/*-{
         grid.checkAll(mode);
     }-*/;

    /**
     * Deletes all rows in grid
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param header -  (boolean) enable/disable cleaning header
     */
    public static native void clearAll(GridObject grid, boolean header)/*-{
        if(header){
            grid.clearAll(header);
        }   else {
            grid.clearAll();
        }
     }-*/;

    /**
     * Clears existing grid state and load data from external file ( xml, json, jsarray, csv )
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param url  - url to external file
     * @param type type of data (xml,csv,json,jsarray) , optional, xml by default
     */
    public static native void clearAndLoad(GridObject grid, String url, String type)/*-{
        grid.clearAndLoad(url, $wnd.doOnAfterRefresh, type);
    }-*/;

    /**
     * removes selection from the grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void clearSelection(GridObject grid)/*-{
        grid.clearSelection();
    }-*/;

    /**
     * Gets all possible values in column
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param column - {number} zero based index of column
     */
    public static native void collectValues(GridObject grid, int column)/*-{
        grid.collectValues(column);
    }-*/;

    /**
     * Copies content of block selection into the clipboard in csv format (delimiter as set for csv serialization)
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void copyBlockToClipboard(GridObject grid)/*-{
       grid.copyBlockToClipboard();
    }-*/;

    /**
     * Copies row content to another existing row
     *
     * @param grid        - JSObject (dhtmlxGrid)
     * @param from_row_id - from row
     * @param to_row_id   - to row
     */
    public static native void copyRowContent(GridObject grid, int from_row_id, int to_row_id)/*-{
        grid.copyRowContent(from_row_id, to_row_id);
    }-*/;

    /**
     * Deletes a row from the grid
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row to delete
     */
    public static native void deleteRow(GridObject grid, int row_id)/*-{
         grid.deleteRow(row_id);
    }-*/;

    /**
     * Deletes selected row(s)
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void deleteSelectedRows(GridObject grid)/*-{
        grid.deleteSelectedRows();
    }-*/;

    /**
     * Destructor, removes grid and cleans used memory
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void destructor(GridObject grid)/*-{
      grid.destructor();
    }-*/;


    /**
     * Removes an event-handler
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param eventId - id of event handler
     */
    public static native void detachEvent(GridObject grid, String eventId)/*-{
      grid.detachEvent(eventId);
    }-*/;

    /**
     * Removes the header line from grid (opposite to attachHeader)
     *
     * @param grid  - JSObject (dhtmlxGrid)
     * @param index - index of row to be removed ( zero based )
     */
    public static native void detachHeader(GridObject grid, int index)/*-{
      grid.detachHeader(index);
    }-*/;

    /**
     * Determines if a row with specified id exists
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row id
     */
    public static native void doesRowExist(GridObject grid, int row_id)/*-{
      grid.doesRowExist(row_id);
    }-*/;

    /**
     * Creates an Editor object and switch a cell to the edit mode if allowed<br>
     * Use selectCell("some_row_ID",cellIndex) before edit cell.
     *
     * @param grid
     */
    public static native void editCell(GridObject grid)/*-{
        grid.editCell();
    }-*/;

    /**
     * Returns a value from editor(if presents) to cell and closes editor
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode -  if true - current edit value will be reverted to previous one
     */
    public static native void editStop(GridObject grid, boolean mode)/*-{
        if(mode){
           grid.editStop(mode);
        } else {
            grid.editStop();
        }
    }-*/;

    /**
     * Sets even/odd rows css styles
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param even - rows
     * @param odd  - rows
     */
    public static native void enableAlterCss(GridObject grid, String even, String odd)/*-{
         grid.enableAlterCss(even, odd);
     }-*/;

    /**
     * Enable auto height of grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableAutoHeight(GridObject grid, boolean mode)/*-{
        grid.enableAutoHeight(mode);
    }-*/;

    /**
     * Enable auto height of grid
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param mode      - true/false
     * @param maxHeight - maximum height before scrolling appears
     */
    public static native void enableAutoHeight(GridObject grid, boolean mode, int maxHeight)/*-{
        grid.enableAutoHeight(mode, maxHeight);
    }-*/;

    /**
     * Enable auto height of grid
     *
     * @param grid            - JSObject (dhtmlxGrid)
     * @param mode            - true/false
     * @param maxHeight       - maximum height before scrolling appears
     * @param countFullHeight - control the usage of maxHeight parameter - when set to true all grid height included in max height calculation, if false then only data part (no header) of grid included in calcualation (false by default)
     */
    public static native void enableAutoHeight(GridObject grid, boolean mode, int maxHeight, boolean countFullHeight)/*-{
        grid.enableAutoHeight(mode, maxHeight, countFullHeight);
    }-*/;

    /**
     * Changes grid's container size on the fly to fit total width of grid columns
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableAutoWidth(GridObject grid, boolean mode)/*-{
        grid.enableAutoWidth(mode);
    }-*/;

    /**
     * Changes grid's container size on the fly to fit total width of grid columns
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param mode      - true/false
     * @param max_limit - max allowed width
     * @param min_limit - min allowed width
     */
    public static native void enableAutoWidth(GridObject grid, boolean mode, int max_limit, int min_limit)/*-{
        grid.enableAutoWidth(mode, max_limit, min_limit);
    }-*/;

    /**
     * Enables the block selection mode in grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableBlockSelection(GridObject grid, boolean mode)/*-{
        grid.enableBlockSelection(mode);
    }-*/;

    /**
     * Enables/disables unique id for cells (id will be automatically created)
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableCellIds(GridObject grid, boolean mode)/*-{
        grid.enableCellIds(mode);
    }-*/;

    /**
     * Enable/disable context menu, if null - context menu will be disabled
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param menu - JSObject (dhtmlxMenu)
     */
    public static native void enableContextMenu(GridObject grid, MenuObject menu)/*-{
        grid.enableContextMenu(menu, "standard");
    }-*/;

    /**
     * Enable/disable drag-and-drop
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - enabled/disabled
     */
    public static native void enableDragAndDrop(GridObject grid, boolean mode)/*-{
        grid.enableDragAndDrop(mode);
    }-*/;

    /**
     * Switches to the mode when dragged item, dropped in target location in the same order as they were in the source grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - enabled/disabled
     */
    public static native void enableDragOrder(GridObject grid, boolean mode)/*-{
        grid.enableDragOrder(mode);
    }-*/;

    /**
     * Enable/disable events which fire excell editing, mutual exclusive with enableLightMouseNavigation
     *
     * @param grid     -     JSObject (dhtmlxGrid)
     * @param click    -    enable/disable editing by single click
     * @param dblclick - enable/disable editing by double click
     * @param f2key    -    enable/disable editing by pressing F2 key
     */
    public static native void enableEditEvents(GridObject grid, boolean click, boolean dblclick, boolean f2key)/*-{
        grid.enableEditEvents(click, dblclick, f2key);
    }-*/;

    /**
     * Enables/disables mode when readonly cell is not available with tab
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableEditTabOnly(GridObject grid, boolean mode)/*-{
        grid.enableEditTabOnly(mode);
    }-*/;

    /**
     * Specifies if values passed to Header are images file names
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableHeaderImages(GridObject grid, boolean mode)/*-{
        grid.enableHeaderImages(mode);
    }-*/;

    /**
     * Enables/disables hot keys in grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableKeyboardSupport(GridObject grid, boolean mode)/*-{
        grid.enableKeyboardSupport(mode);
    }-*/;

    /**
     * Enables/disables the light mouse navigation mode (row selection with mouse over, editing with single click), mutual exclusive with enableEditEvents
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableLightMouseNavigation(GridObject grid, boolean mode)/*-{
        grid.enableLightMouseNavigation(mode);
    }-*/;


    /**
     * Sets the marked cells support to enabled or disabled state
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableMarkedCells(GridObject grid, boolean mode)/*-{
        grid.enableMarkedCells(mode);
    }-*/;

    /**
     * Sets the multiline rows support to enabled or disabled state
     *
     * @param grid  - JSObject (dhtmlxGrid)
     * @param state - true/false
     */
    public static native void enableMultiline(GridObject grid, boolean state)/*-{
        grid.enableMultiline(state);
    }-*/;

    /**
     * Sets the multiselect mode to enabled or disabled state
     *
     * @param grid  - JSObject (dhtmlxGrid)
     * @param state - true/false
     */
    public static native void enableMultiselect(GridObject grid, boolean state)/*-{
        grid.enableMultiselect(state);
    }-*/;

    /**
     * Enables/disables resizing for specified columns
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param list - list of true/false values separated with default delimiter (comma), resizing enabled for all columns by default
     */
    public static native void enableResizing(GridObject grid, String list)/*-{
        grid.enableResizing(list);
    }-*/;


    /**
     * Enables/disables hovering row on mouse over
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param state  - true/false
     * @param cssCls - css class for hovering row
     */
    public static native void enableRowsHover(GridObject grid, boolean state, String cssCls)/*-{
        grid.enableRowsHover(state, cssCls);
     }-*/;

    /**
     * enables the smart rendering mode
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param mode   - true/false
     * @param buffer - has sense only in dynamic loading mode, count of rows requested from server by single operation
     */
    public static native void enableSmartRendering(GridObject grid, boolean mode, int buffer)/*-{
        if(mode){
           grid.enableSmartRendering(mode, buffer);
        } else {
           grid.enableSmartRendering(mode);
        }
     }-*/;

    /**
     * Enables stable sorting algorithm
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void enableStableSorting(GridObject grid, boolean mode)/*-{
        grid.enableStableSorting(mode);
    }-*/;

    /**
     * Enables/disables tooltips for specified columns
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param list - list of true/false values separated with default delimiter (comma), tooltip enabled for all columns by default
     */
    public static native void enableTooltips(GridObject grid, String list)/*-{
        grid.enableTooltips(list);
    }-*/;

    /**
     * Affects the block selection, so it will copy|paste only visible text , not values behind
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true/false
     */
    public static native void forceLabelSelection(GridObject grid, boolean mode)/*-{
        grid.forceLabelSelection(mode);
    }-*/;

    /**
     * Gets a list of all row ids in grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return JavaScriptObject -  list of row ids with comma delimiter
     */
    public static native JavaScriptObject getAllRowIds(GridObject grid)/*-{
        return grid.getAllRowIds();
    }-*/;

    /**
     * Get list of Ids of all rows with checked exCell in specified column
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - column index
     * @return JavaScriptObject -  list of row ids with comma delimiter
     */
    public static native JavaScriptObject getCheckedRows(GridObject grid, int col_ind)/*-{
        return grid.getCheckedRows(col_ind);
    }-*/;

    /**
     * Gets a column index by column id
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param id   - column id
     * @return column index
     */
    public static native String getColIndexById(GridObject grid, String id)/*-{
        return grid.getColIndexById(id);
    }-*/;

    /**
     * Gets a column type by column index
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - column index
     * @return column type
     */
    public static native String getColType(GridObject grid, int col_ind)/*-{
        return grid.getColType(col_ind);
    }-*/;

    /**
     * Gets a column type by column ID
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param id   - column id
     * @return column type
     */
    public static native String getColTypeById(GridObject grid, String id)/*-{
        return grid.getColTypeById(id);
    }-*/;

    /**
     * Gets the width of specified column in pixels
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - column index
     * @return column width
     */
    public static native String getColWidth(GridObject grid, int col_ind)/*-{
        return grid.getColWidth(col_ind);
    }-*/;

    /**
     * Gets the width of specified column in pixels
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - column index
     * @return column width
     */
    public static native int getColWidthAsInteger(GridObject grid, int col_ind)/*-{
        return grid.getColWidth(col_ind);
    }-*/;

    /**
     * Gets the column id of column specified by index
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - column index
     * @return column id
     */
    public static native String getColumnId(GridObject grid, int col_ind)/*-{
        return grid.getColumnId(col_ind);
    }-*/;

    /**
     * Gets label of column specified by index
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - column index
     * @return column label
     */
    public static native String getColumnLabel(GridObject grid, int col_ind)/*-{
        return grid.getColumnLabel(col_ind);
    }-*/;


    /**
     * Gets number of columns in grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return number of columns in grid (including hidden columns)
     */
    public static native int getColumnsNum(GridObject grid)/*-{
        return grid.getColumnsNum();
    }-*/;

    /**
     * Gets a Combo object of specified column. Uses it to change a select box value for a cell before opening editor
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - index of the column to get combo object for. Note column must have "co", "coro" or "combo" type
     * @return Combo object
     */
    public static native JavaScriptObject getCombo(GridObject grid, int col_ind)/*-{
        return grid.getCombo(col_ind);
    }-*/;

    /**
     * Gets combobox specific for the cell in question
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param row_id  - row id
     * @param col_ind - column index. Note cell must have "co", "coro" or "combo" type
     * @return Combo object
     */
    public static native JavaScriptObject getCustomCombo(GridObject grid, String row_id, int col_ind)/*-{
        grid.getCustomCombo(row_id, col_ind);
    }-*/;

    /**
     * Gets input object of filter linked to column in question<br>
     * The method works only for filter-elements, so it will not work for #text_search.
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - index of column
     * @return input object of filter
     */
    public static native JavaScriptObject getFilterElement(GridObject grid, int col_ind)/*-{
        return grid.getFilterElement(col_ind);
    }-*/;

    /**
     * Gets the label of the footer specified by index
     *
     * @param grid     - JSObject (dhtmlxGrid)
     * @param col_ind  - index of column
     * @param frow_ind - footer row index
     * @return footer label
     */
    public static native String getFooterLabel(GridObject grid, int col_ind, int frow_ind)/*-{
        return grid.getFooterLabel(col_ind, frow_ind);
    }-*/;

    /**
     * Returns the array of marked cells (pairs of row id and column index)
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return array //TODO: must be tested
     */
    public static native JavaScriptObject getMarked(GridObject grid)/*-{
        return grid.getMarked();
     }-*/;

    /**
     * Returns row attribute value which was set in the <row> xml tag
     *
     * @param grid  - JSObject (dhtmlxGrid)
     * @param rowId - row id
     * @param name  - attribute name
     * @return attribute value
     */
    public static native String getRowAttribute(GridObject grid, String rowId, String name)/*-{
        return grid.getRowAttribute(rowId, name);
    }-*/;

    /**
     * Returns row id or null if there is no row with specified index
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param row_ind - row index
     * @return row id
     */
    public static native String getRowId(GridObject grid, int row_ind)/*-{
        return grid.getRowId(row_ind);
    }-*/;

    /**
     * Gets row index by id (grid only)
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row id
     * @return row index
     */
    public static native int getRowIndex(GridObject grid, String row_id)/*-{
        return grid.getRowIndex(row_id);
    }-*/;

    /**
     * Returns count of row in grid (in case of dynamic mode it will return expected count of rows)
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return count of row
     */
    public static native int getRowsNum(GridObject grid)/*-{
        return  grid.getRowsNum();
    }-*/;

    /**
     * Returns index of selected cell or -1 if there is no selected sell
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return index of selected cell
     */
    public static native int getSelectedCellIndex(GridObject grid)/*-{
        return grid.getSelectedCellIndex();
    }-*/;

    /**
     * Returns id of selected row (list of ids with default delimiter) or null if non row selected
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return ID of selected cell
     */
    public static native String getSelectedRowId(GridObject grid)/*-{
        return grid.getSelectedRowId();
    }-*/;

    /**
     * Returns array, first element is index of sorted column, second - direction of sorting (“asc” or “des”).
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return “asc” or “des” //TODO: Array mus be tested
     */
    public static native JavaScriptObject getSortingState(GridObject grid)/*-{
        return grid.getSortingState();
    }-*/;

    /**
     * Returns array with details about current grid state
     * If paging mode is enabled:
     * state[0] = number of current active page
     * state[1] = index of the top row in the page
     * state[2] = index of the last row in the page
     * state[3] = number of rows in grid
     * <p/>
     * If paging mode is disabled:
     * state[0] = index of the top visible row
     * state[1] = height of the visible area in rows
     * state[2] = number of rows in grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return state[]      //TODO: array mus be tested
     */
    public static native JavaScriptObject getStateOfView(GridObject grid)/*-{
        return grid.getStateOfView();
    }-*/;

    /**
     * Hide pivot table related to grid, if any exists, switch grid back to normal mode
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void hidePivot(GridObject grid)/*-{
        grid.hidePivot();
    }-*/;

    /**
     * Initialize grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void init(GridObject grid)/*-{
        grid.init();
    }-*/;

    /**
     * @param grid - JSObject (dhtmlxGrid)
     * @param url  - url to external file
     * @param type - type of data (xml,csv,json,jsarray) , optional, xml by default
     */
    public static native void load(GridObject grid, String url, DataType type)/*-{
        grid.load(data, type);
    }-*/;

    /**
     * Locks/unlocks a row for editing
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - id of row
     * @param mode   - true/false - lock/unlock
     */
    public static native void lockRow(GridObject grid, String row_id, boolean mode)/*-{
        grid.lockRow(row_id, mode);
    }-*/;

    /**
     * Sets selection or removes selection from specified cell
     *
     * @param grid     - JSObject (dhtmlxGrid)
     * @param row_ind  - row index
     * @param cell_ind - cell index
     * @param state    - true / false
     */
    public static native void mark(GridObject grid, int row_ind, int cell_ind, boolean state)/*-{
        grid.markers(row_ind, cell_ind, state);
    }-*/;

    /**
     * Moves a row one position down if it's possible
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row id
     */
    public static native void moveRowDown(GridObject grid, String row_id)/*-{
        grid. moveRowDown(row_id);
    }-*/;

    /**
     * Moves a row one position up if possible
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row id
     */
    public static native void moveRowUp(GridObject grid, String row_id)/*-{
        grid.moveRowUp(row_id);
    }-*/;

    /**
     * Loads data from local datasource ( xml string, csv string, xml island, xml object, json objecs , javascript array )
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param data - data string
     * @param type - {@link com.ghtmlx.client.util.DataType}
     */
    public static native void parse(GridObject grid, String data, DataType type)/*-{
        grid.parse(data, type);
    }-*/;

    /**
     * Pastes content of clipboard into the block selection of grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void pasteBlockFromClipboard(GridObject grid)/*-{
        grid.pasteBlockFromClipboard();
    }-*/;

    /**
     * Prevent caching in IE by adding random values to URL string
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void preventIECaching(GridObject grid)/*-{
        grid.preventIECaching();
    }-*/;

    /**
     * Redefine this method in your code to define how grid row values should be displaied while draging
     *
     * @param grid     - JSObject (dhtmlxGrid)
     * @param image    - image
     * @param row_id   - row_id
     * @param cell_ind - cell id to display cell value
     */
    public static native void rowToDragElement(GridObject grid, Image image, String row_id, int cell_ind)/*-{
        grid.rowToDragElement = function(row_id){
            return image + grid.cells(row_id, cell_ind).getValue();
        }
    }-*/;

    /**
     * Selects all rows in grid, it doesn't fire any events
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void selectAll(GridObject grid)/*-{
        grid.selectAll();
    }-*/;

    /**
     * Set selection to specified row-cell
     *
     * @param grid          - JSObject (dhtmlxGrid)
     * @param row_ind       - row index
     * @param cell_ind      - cell index
     * @param call_onSelect - if true, then call function on select
     * @param preserve      - preserve previously selected rows true/false. Multiselect mode should be enabled.
     * @param edit          - switch selected cell to edit mode
     * @param show          - true/false - scroll row to view
     */
    public static native void selectCell(GridObject grid, String row_ind, int cell_ind, boolean call_onSelect, boolean preserve, boolean edit, boolean show)/*-{
        grid.selectCell(row_ind, cell_ind, call_onSelect, preserve, edit, show);
    }-*/;

    /**
     * Selects row (and first cell of it)
     *
     * @param grid          - JSObject (dhtmlxGrid)
     * @param row_ind       - row index
     * @param call_onSelect - if true, then call function on select
     * @param preserve      - preserve previously selected rows true/false. Multi select mode should be enabled.
     * @param show          - true/false - scroll row to view
     */
    public static native void selectRow(GridObject grid, int row_ind, boolean call_onSelect, boolean preserve, boolean show)/*-{
        grid.selectRow(row_ind, call_onSelect, preserve, show);
    }-*/;

    /**
     * Selects row by ID
     *
     * @param grid          - JSObject (dhtmlxGrid)
     * @param row_id        - row id
     * @param call_onSelect - true if to call onRowSelect
     * @param preserve      - preserve previously selected
     * @param show          - true/false - scroll row to view
     */
    public static native void selectRowById(GridObject grid, String row_id, boolean call_onSelect, boolean preserve, boolean show)/*-{
        grid.selectRowById(row_id, preserve, show, call_onSelect);
    }-*/;

    /**
     * Set height which will be used in smart rendering mode for row calculation, function need to be used if you use custom skin for grid which changes default row height
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param height - awaited height of row
     */
    public static native void setAwaitedRowHeight(GridObject grid, int height)/*-{
        grid.setAwaitedRowHeight(height);
    }-*/;

    /**
     * Change csv delimiter
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param delimiter - delimer used in CSV operations, comma by default
     */
    public static native void setCSVDelimiter(GridObject grid, char delimiter)/*-{
        grid.setCSVDelimiter(delimiter);
    }-*/;

    /**
     * Sets style to cell
     * {@code setCellTextStyle("row1",0, "color:red;border:1pxsolidgray;")}
     *
     * @param grid     - JSObject (dhtmlxGrid)
     * @param row_id   - row id
     * @param cell_ind - cell index
     * @param style    - css style
     */
    public static native void setCellTextStyle(GridObject grid, String row_id, int cell_ind, String style)/*-{
        grid.setCellTextStyle(row_id, cell_ind, style);
   }-*/;

    /**
     * Set value for all checkboxes in specified column
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - column index
     * @param checked - true is checked
     */
    public static native void setCheckedRows(GridObject grid, int col_ind, boolean checked)/*-{
        var v = 0;
        if(checked){
           v = 1;
        }
        grid.setCheckedRows()
    }-*/;

    /**
     * Set align of values in columns
     *
     * @param grid       - JSObject (dhtmlxGrid)
     * @param alignArray - list of align values (possible values are: right, left, center, justify)
     */
    public static native void setColAlign(GridObject grid, String alignArray)/*-{
        grid.setColAlign(alignArray);
    }-*/;

    /**
     * Set column sort types (avaialble: str, int, date, na
     *
     * @param grid         - JSObject (dhtmlxGrid)
     * @param sortingArray - sort codes list
     */
    public static native void setColSorting(GridObject grid, String sortingArray)/*-{
        grid.setColSorting(sortingArray);
    }-*/;

    /**
     * Set column types
     *
     * @param grid       - JSObject (dhtmlxGrid)
     * @param typesArray - type codes list
     */
    public static native void setColTypes(GridObject grid, String typesArray)/*-{
        grid.setColTypes(typesArray);
    }-*/;

    /**
     * Set vertical align of columns
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param list - vertical align values list for columns (possible values are: baseline, sub, super, top, text-top, middle, bottom, text-bottom)
     */
    public static native void setColVAlign(GridObject grid, String list)/*-{
        grid.setColVAlign(list);
    }-*/;

    /**
     * Sets width of specified column in pixels (not works with procent based grid)
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - collumn index
     * @param width   - new width value (ex. "150")
     */
    public static native void setColWidth(GridObject grid, int col_ind, String width)/*-{
        grid.setColWidth(col_ind, width);
    }-*/;


    /**
     * Set collumn collor
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param list - array with colors, like this {@code "white, #d5f1ff, #d5f1ff"},  set colors for the first 3 columns
     */
    public static native void setColumnColor(GridObject grid, String list)/*-{
        grid.setColumnColor(list)
    }-*/;

    /**
     * Sets ids to specified column
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param col_ind - index of column
     * @param id      - id of column
     */
    public static native void setColumnId(GridObject grid, int col_ind, String id)/*-{
        grid.setColumnId(col_ind, id);
    }-*/;

    /**
     * Sets ids to every column. Can be used then to retreive the index of the desired colum
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param ids  -  list of ids or empty if to use values set earlier
     */
    public static native void setColumnIds(GridObject grid, String ids)/*-{
        grid.setColumnIds(ids);
    }-*/;

    /**
     * Sets new column header label
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param col_ind   - header column index
     * @param label     - new label for the cpecified header's column. Can contain {@code "img:[../codebase/imgs/image.gif]New Label"}
     * @param h_row_ind - header row index (default is 1)
     */
    public static native void setColumnLabel(GridObject grid, int col_ind, String label, int h_row_ind)/*-{
        grid.setColumnLabel(col_ind, label, h_row_ind);
    }-*/;

    /**
     * Set minimum column width (works only for manual resizing)
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param width   - minimum column width, can be set for specified column
     * @param col_ind - column index
     */
    public static native void setColumnMinWidth(GridObject grid, int width, int col_ind)/*-{
        grid.setColumnMinWidth(width, col_ind);
    }-*/;

    /**
     * Set minimum column width (works only for manual resizing)
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param list - list of column min widths, like this {@code "50,100,100,150,200,100,150"}
     */
    public static native void setColumnMinWidths(GridObject grid, String list)/*-{
        grid.setColumnMinWidth(list);
    }-*/;

    /**
     * Set delimiter character used in list values (default is ”,”)
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param delimiter - delimiter as char
     */
    public static native void setDelimiter(GridObject grid, char delimiter)/*-{
        grid.setDelimiter(delimiter);
    }-*/;

    /**
     * Set Drag-And-Drop behavior
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - behavior name
     *             {@code child} - the item is dropped as a child of the target item
     *             {@code sibling} - the item is dropped as a sibling of the target item; if drop occurs between the parent and the child elements, the item is moved on the parent level
     *             {@code complex}  -  the item is dropped as a sibling or a child depending on the position of the target item towards the landing; if drop occurs between the parent and the child elements, the item is moved on the parent level
     *             {@code sibling-next} - the item is dropped as a sibling of the target; if drop occurs between the parent and the child elements, the item is moved on the child level
     *             {@code complex-next} - the item is dropped as a sibling or a child depending on the position of the target item towards the landing; if drop occurs between the parent and the child elements, the item is moved on the child level
     */
    public static native void setDragBehavior(GridObject grid, String mode)/*-{
        grid.setDragBehavior(mode);
    }-*/;

    /**
     * Define text (single and plural forms) for extended visual appearence of drag-n-drop
     * Required library file: dhtmlx_extdrag.js
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param single - single form (like “product”)
     * @param plural - plural form (if omitted, then “s” will be added to single form)
     */
    public static native void setDragText(GridObject grid, String single, String plural)/*-{
        grid.setDragText(single, plural);
    }-*/;

    /**
     * Manage editibility of the grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - set not editable if FALSE, set editable otherwise
     */
    public static native void setEditable(GridObject grid, boolean mode)/*-{
        grid.setEditable(mode);
    }-*/;

    /**
     * Sets elements which get focus when tab is pressed in the last or first (tab+shift) cell
     *
     * @param grid     - JSObject (dhtmlxGrid)
     * @param start_id - gets focus when tab+shift are pressed in the first cell
     * @param end_id   - gets focus when tab is pressed in the last cell
     */
    public static native void setExternalTabOrder(GridObject grid, String start_id, String end_id)/*-{
        grid.setExternalTabOrder(start_id, end_id);
    }-*/;

    /**
     * Sets new label for cell in footer
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param label   - new label for the cpecified footer's column. Can contain {@code img:[../codebase/imgs/image.gif]New Label}
     * @param row_ind - footer row index (1 by default)
     */
    public static native void setFooterLabel(GridObject grid, int col_ind, String label, int row_ind)/*-{
        grid.setFooterLabel(col_ind,  label, row_ind);
    }-*/;

    /**
     * Set header label and default params for new headers
     *
     * @param grid        - JSObject (dhtmlxGrid)
     * @param headerArray - labels
     */
    public static native void setHeader(GridObject grid, String headerArray)/*-{
        grid.setHeader(headerArray);
    }-*/;

    /**
     * Set path to external images used in grid ( tree and img column types )
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param path - url (or relative path) of images folder {@code "../codebase/imgs/icons_greenfolders/"}
     */
    public static native void setIconPath(GridObject grid, String path)/*-{
        grid.setIconPath(path);
    }-*/;

    /**
     * Set path to grid internal images (sort direction, any images used in editors, checkbox, radiobutton)
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param imagePath - url (or relative path) of images folder, example: {@code "js/dhtmlxSuite/dhtmlxGrid/codebase/imgs/"}
     */
    public static native void setImagePath(GridObject grid, String imagePath)/*-{
        grid.setImagePath(imagePath);
    }-*/;

    /**
     * Set width of columns in pixels
     *
     * @param grid        - JSObject (dhtmlxGrid)
     * @param widthsArray - list of column width in pixels
     */
    public static native void setInitWidths(GridObject grid, String widthsArray)/*-{
        grid.setInitWidths(widthsArray);
    }-*/;

    /**
     * Set width of columns in percents
     *
     * @param grid        - JSObject (dhtmlxGrid)
     * @param widthsArray - list of column width in percents. Sum of the column widths must be equal to 100
     */
    public static native void setInitWidthsP(GridObject grid, String widthsArray)/*-{
        grid.setInitWidths(widthsArray);
    }-*/;

    /**
     * Create grid without header. Call before initialization, but after setHeader. setHeader have to be called in any way as it defines number of columns
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - true to use no header in the grid
     */
    public static native void setNoHeader(GridObject grid, boolean mode)/*-{
        grid.setNoHeader(mode);
    }-*/;

    /**
     * Allows to set custom paging skin
     * NOT IMPLEMENTED!!!
     * Required library file: dhtmlxgrid_pgn.js
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param name - skin name (default, toolbar, bricks)
     */
    public static native void setPagingSkin(GridObject grid, String name)/*-{
        grid.setPagingSkin(name);
    }-*/;

    /**
     * Sets row attribute {@link "http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:api_method_dhtmlxgridobject_setrowattribute"}
     *
     * @param grid          - JSObject (dhtmlxGrid)
     * @param row_id        - row ID
     * @param attributeName - attribute name
     * @param value         - new attribute's value
     */
    public static native void setRowAttribute(GridObject grid, String row_id, String attributeName, String value)/*-{
        grid.setRowAttribute(row_id, attributeName, value);
    }-*/;

    /**
     * Sets background color of row (via bgcolor attribute)
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row id
     * @param color  - color value
     */
    public static native void setRowColor(GridObject grid, String row_id, String color)/*-{
        grid.setRowColor(row_id, color);
    }-*/;

    /**
     * hide/show row (warning! - this command doesn't affect row indexes, only visual appearance)
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row ID
     * @param state  - true/false - hide/show row
     */
    public static native void setRowHidden(GridObject grid, String row_id, boolean state)/*-{
        grid.setRowHidden(row_id, state);
    }-*/;

    /**
     * Sets new id for row by its index
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param row_ind - row index
     * @param row_id  - new row id
     */
    public static native void setRowId(GridObject grid, int row_ind, String row_id)/*-{
        grid.setRowId(row_ind, row_id);
    }-*/;

    /**
     * Sets row text BOLD
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row id
     */
    public static native void setRowTextBold(GridObject grid, String row_id)/*-{
        grid. setRowTextBold(row_id);
    }-*/;

    /**
     * Sets row text NORMAL
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row id
     */
    public static native void setRowTextNormal(GridObject grid, String row_id)/*-{
        grid. setRowTextNormal(row_id);
    }-*/;

    /**
     * Sets style to row
     *
     * @param grid     - JSObject (dhtmlxGrid)
     * @param row_id   - row id
     * @param rowStyle - style string in common format (exmpl: {@code "color:red; border:1px solid gray;"})
     */
    public static native void setRowTextStyle(GridObject grid, String row_id, String rowStyle)/*-{
        grid.setRowTextStyle(row_id, rowStyle);
    }-*/;

    /**
     * Set one of predefined css styles (xp, mt, gray, light, clear, modern, sb_dark)
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param skin - skin name
     */
    public static native void setSkin(GridObject grid, String skin)/*-{
        grid.setSkin(skin);
    }-*/;

    /**
     * Sets position and visibility of sort arrow
     *
     * @param grid    - JSObject (dhtmlxGrid)
     * @param state   - true/false - show/hide image
     * @param col_ind - index of column
     * @param order   - asc/desc - type of image (optional, "asc" by default)
     * @param row     - one based index of header row (used in multirow headers, top row by default)
     */
    public static native void setSortImgState(GridObject grid, boolean state, int col_ind, String order, int row)/*-{
        grid.setSortImgState(state, col_ind, order, row);
    }-*/;

    /**
     * Modify default style of grid and its elements. Call before or after Init
     *
     * @param grid              - JSObject (dhtmlxGrid)
     * @param headerStyle       - style def. expression for header
     * @param cellsStyle        - style def. expression for grid cells
     * @param selectedCellStyle - style def. expression for selected cell
     * @param selectedRowStyle  - style def. expression for selected Row
     */
    public static native void setStyle(GridObject grid, String headerStyle, String cellsStyle, String selectedCellStyle, String selectedRowStyle)/*-{
        grid.setStyle(headerStyle, cellsStyle, selectedCellStyle, selectedRowStyle);
    }-*/;

    /**
     * Set tab order of columns
     *
     * @param grid     - JSObject (dhtmlxGrid)
     * @param tabOrder - list of tab indexes
     */
    public static native void setTabOrder(GridObject grid, String tabOrder)/*-{
        grid.setTabOrder(tabOrder);
    }-*/;

    /**
     * Scrolls row to the visible area
     *
     * @param grid   - JSObject (dhtmlxGrid)
     * @param row_id - row id
     */
    public static native void showRow(GridObject grid, String row_id)/*-{
        grid.showRow(row_id);
    }-*/;

    /**
     * Sorting grid
     *
     * @param grid      - JSObject (dhtmlxGrid)
     * @param col_ind   - index of column, by which grid need to be sorted
     * @param sortType  - sorting type (str,int,date), optional, by default sorting type taken from column setting
     * @param sortOrder - sorting order (asc,des), optional, by default sorting order based on previous sorting operation
     */
    public static native void sortRows(GridObject grid, int col_ind, String sortType, String sortOrder)/*-{
        grid.sortRows(col_ind, sortType, sortOrder);
    }-*/;

    /**
     * Start fast operation mode, in such mode events are not generated, some time consuming actions applied only once, which allow to increase performance
     * NOT IMPLEMENTED!!!
     * Required library file: dhtmlxgrid_fast.js
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void startFastOperations(GridObject grid)/*-{
        grid.startFastOperations();
    }-*/;

    /**
     * Turn off fast operation mode, need to be executed to normalize view.
     * NOT IMPLEMENTED!!!
     * Required library file: dhtmlxgrid_fast.js
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void stopFastOperations(GridObject grid)/*-{
        grid.stopFastOperations();
    }-*/;

    /**
     * Include additional data with info about which rows was added and which deleted, enabled by default
     * NOT IMPLEMENTED!!!
     * Required library file: dhtmlxgrid_form.js
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - enable/disable mode
     */
    public static native void submitAddedRows(GridObject grid, boolean mode)/*-{
        grid.submitAddedRows(mode);
    }-*/;


    /**
     * Include only changed rows in form submit
     * NOT IMPLEMENTED!!!
     * Required library file: dhtmlxgrid_form.js
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - enable/disable mode
     */
    public static native void submitOnlyChanged(GridObject grid, boolean mode)/*-{
        grid.submitOnlyChanged(mode);
    }-*/;

    /**
     * Include only row's IDS in form submit
     * NOT IMPLEMENTED!!!
     * Required library file: dhtmlxgrid_form.js
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - enable/disable mode
     */
    public static native void submitOnlyRowID(GridObject grid, boolean mode)/*-{
        grid.submitOnlyRowID(mode);
    }-*/;

    /**
     * Include only selected rows in form submit
     * NOT IMPLEMENTED!!!
     * Required library file: dhtmlxgrid_form.js
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - enable/disable mode
     */
    public static native void submitOnlySelected(GridObject grid, boolean mode)/*-{
        grid.submitOnlySelected(mode);
    }-*/;

    /**
     * Include serialized grid as part of form submit
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @param mode - enable/disable mode
     */
    public static native void submitSerialization(GridObject grid, boolean mode)/*-{
        grid.submitSerialization(mode);
    }-*/;

    /**
     * Returns unique ID
     *
     * @param grid - JSObject (dhtmlxGrid)
     * @return ID
     */
    public static native String uid(GridObject grid)/*-{
       return grid.uid();
    }-*/;

    /**
     * Uncheck all checkboxes in grid
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void uncheckAll(GridObject grid)/*-{
        grid.uncheckAll();
    }-*/;

    /**
     * Removes selection from all marked cell
     * NOT IMPLEMENTED!!!
     * Required library file: dhtmlxgrid_markers.js
     *
     * @param grid - JSObject (dhtmlxGrid)
     */
    public static native void unmarkAll(GridObject grid)/*-{
        grid.unmarkAll();
    }-*/;

    /**
     * Refresh grid from XML ( doesnt work for buffering, tree grid or rows in smart rendering mode )
     *
     * @param grid  - JSObject (dhtmlxGrid)
     * @param url  - url to the file
     * @param insert_new_item  - insert new items (optional, true by default)
     * @param del_missed - delete missed rows (optional, false by default)
     * @param forceEvent - function, will be executed after refresh completted (doAfterUpdate)
     */
    public static native void updateFromXML(GridObject grid, String url, boolean insert_new_item, boolean del_missed, boolean forceEvent)/*-{
    if(forceEvent){
        grid.updateFromXML(url, insert_new_item, del_missed);
    }   else{
        grid.updateFromXML(url, insert_new_item, del_missed, $wnd.doOnAfterUpdate);
    }
    }-*/;
}
